package com.example.lance.textapp;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.KeyEvent;

/**
 * Created by lance on 15/5/8.
 */
public class TextUtil {

    private TextInfo mTextInfo = null;
    private String mStrText = "";
    private Vector mString = null;
    private Paint mPaint = null;

    public TextUtil(String StrText, int x, int y, int w, int h,
                    int textcolor, int alpha, int textsize, Paint.Align malgin) {
        mPaint = new Paint();
        mString = new Vector();
        this.mStrText = StrText;

        this.mTextInfo = new TextInfo();
        this.mTextInfo.mTextPosx = x;
        this.mTextInfo.mTextPosy = y;
        this.mTextInfo.mTextWidth = w;
        this.mTextInfo.mTextHeight = h;
        this.mTextInfo.mFontColor = textcolor;
        this.mTextInfo.mAlpha = alpha;
        this.mTextInfo.mTextSize = textsize;
        this.mTextInfo.mAlgin = malgin;
    }

    public void InitText() {
        mString.clear();// 清空Vector

        mPaint.setTextSize(this.mTextInfo.mTextSize);
        mPaint.setColor(Color.BLUE);
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        mPaint.setTextAlign(this.mTextInfo.mAlgin);

        this.buildeTextInfo();
    }

    /**
     * 得到字符串信息包括行数，页数等信息
     */

    public void buildeTextInfo() {
        char ch;
        int w = 0;
        int istart = 0;
        FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性
        this.mTextInfo.mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度
        this.mTextInfo.mPageLineNum = this.mTextInfo.mTextHeight / this.mTextInfo.mFontHeight;// 获得行数
        int count = this.mStrText.length();
        for (int i = 0; i < count; i++) {
            ch = this.mStrText.charAt(i);
            float[] widths = new float[1];
            String str = String.valueOf(ch);
            mPaint.getTextWidths(str, widths);
            if (ch == '\n') {
                this.mTextInfo.mRealLine++;// 真实的行数加一
                mString.addElement(this.mStrText.substring(istart, i));
                istart = i + 1;
                w = 0;
            } else {
                w += (int) Math.ceil(widths[0]);
                if (w > this.mTextInfo.mTextWidth) {
                    this.mTextInfo.mRealLine++;// 真实的行数加一
                    mString.addElement(this.mStrText.substring(istart, i));
                    istart = i;
                    i--;
                    w = 0;
                } else {
                    if (i == count - 1) {
                        this.mTextInfo.mRealLine++;// 真实的行数加一
                        mString.addElement(this.mStrText.substring(istart,
                                count));
                    }
                }
            }
        }
    }

    /**
     * 绘制字符串
     *
     * @param canvas
     */
    public void DrawText(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        canvas.drawCircle(this.mTextInfo.mTextPosx, this.mTextInfo.mTextPosy, 5, paint);

        for (int i = this.mTextInfo.mCurrentLine, j = 0; i < this.mTextInfo.mRealLine; i++, j++) {
            if (j > this.mTextInfo.mPageLineNum) {
                break;
            }
            canvas.drawText((String) (mString.elementAt(i)), this.mTextInfo.mTextPosx,
                    this.mTextInfo.mTextPosy + this.mTextInfo.mFontHeight * j, mPaint);
        }
    }

    /**
     * 翻页等按键处理
     *
     * @param keyCode
     * @param event
     * @return
     */
    public boolean KeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
            if (this.mTextInfo.mCurrentLine > 0) {
                this.mTextInfo.mCurrentLine--;
            }
        } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
            if ((this.mTextInfo.mCurrentLine + this.mTextInfo.mPageLineNum) < (this.mTextInfo.mRealLine - 1)) {
                this.mTextInfo.mCurrentLine++;
            }
        }
        return false;
    }

}

